/**
 * Copyright 2015-现在 广州市领课网络科技有限公司
 */
package com.roncoo.education.course.service.api.callback;

import java.util.HashMap;
import java.util.Map;

import com.roncoo.education.util.base.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.roncoo.education.course.service.biz.callback.CallbackOrderBiz;
import com.roncoo.education.course.service.common.bo.OrderInfoPayNotifyBO;
import com.roncoo.education.course.service.common.bo.callback.CallbackOrderBO;
import com.roncoo.education.system.common.bean.vo.SysVO;
import com.roncoo.education.system.feign.IBossSys;
import com.roncoo.education.util.base.BaseController;
import com.roncoo.education.util.enums.OrderStatusEnum;
import com.roncoo.education.util.enums.TradeStatusEnum;
import com.roncoo.education.util.pay.MerchantApiUtil;
import com.xiaoleilu.hutool.util.ObjectUtil;

import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

/**
 * 订单信息表
 *
 * @author wujing123
 */
@RestController
public class CallbackOrderController extends BaseController {

	@Autowired
	private CallbackOrderBiz biz;

	@Autowired
	private IBossSys bossSys;


	@ApiOperation(value = "PayJs支付回调接口-过时", notes = "PayJs支付回调接口-过时")
	@RequestMapping(value = "/notify/payjs")
	public Result<String> payJsCallBack(HttpServletRequest request){
		Map<String, String[]> parameterMap = request.getParameterMap();

		StringBuffer sb = new StringBuffer();
		for (Map.Entry<String,String[]> me : parameterMap.entrySet()){
			sb.append(me.getKey() + " | " + me.getValue()[0]);
		}
		logger.warn(sb.toString());
		logger.warn(bossSys.getSys().toString());
		OrderInfoPayNotifyBO payNotifyBO = new OrderInfoPayNotifyBO();
		return Result.success(bossSys.getSys().toString());
	}

	@ApiOperation(value = "PayJs支付回调接口", notes = "PayJs支付回调接口")
	@RequestMapping("/callback/order/payjs")
	public String payNotify(String outOrderNumber, String return_code, String total_fee,
							String out_trade_no, String payjs_order_id, String transaction_id, String time_end,
							String openid, String mchid, String sign){

		StringBuffer sb = new StringBuffer();
		sb.append("收到通知-----")
				.append("outOrderNumber:"+outOrderNumber)
				.append("return_code:"+return_code)
				.append("total_fee:"+total_fee)
				.append("out_trade_no:"+out_trade_no)
				.append("payjs_order_id:"+payjs_order_id)
				.append("transaction_id:"+transaction_id)
				.append("time_end:"+time_end)
				.append("openid:"+openid)
				.append("mchid:"+mchid)
				.append("sign:"+sign);
		logger.warn(sb.toString());

		OrderInfoPayNotifyBO payNotifyBO = new OrderInfoPayNotifyBO();
		payNotifyBO.setSerialNumber(Long.valueOf(out_trade_no));
		if("1".equals(return_code)){
			payNotifyBO.setOrderStatus(OrderStatusEnum.SUCCESS.getCode());
		}else{
			payNotifyBO.setOrderStatus(OrderStatusEnum.FAIL.getCode());
		}

		return biz.payNotify(payNotifyBO).getData();
	}

	/**
	 * 龙果支付回调
	 *
	 * @param CallbackOrderBO
	 * @return
	 */
	@ApiOperation(value = "龙果支付回调接口", notes = "龙果支付回调接口")
	@RequestMapping(value = "/callback/order/roncoo", method = { RequestMethod.POST, RequestMethod.GET })
	public String roncooPayNotify(@ModelAttribute CallbackOrderBO callbackOrderBO) {
		logger.info("龙果支付回调结果：{}", callbackOrderBO);
		// sign校验
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("payKey", callbackOrderBO.getPayKey());
		paramMap.put("productName", callbackOrderBO.getProductName());
		paramMap.put("outTradeNo", callbackOrderBO.getOutTradeNo());
		paramMap.put("orderPrice", callbackOrderBO.getOrderPrice());
		paramMap.put("productType", callbackOrderBO.getProductType());
		paramMap.put("tradeStatus", callbackOrderBO.getTradeStatus());
		paramMap.put("successTime", callbackOrderBO.getSuccessTime());// 商品名称
		paramMap.put("orderTime", callbackOrderBO.getOrderTime());
		paramMap.put("trxNo", callbackOrderBO.getTrxNo());
		paramMap.put("remark", callbackOrderBO.getRemark());

		SysVO sys = bossSys.getSys();
		if (ObjectUtil.isNull(sys)) {
			return "找不到系统配置信息";
		}
		if (StringUtils.isEmpty(sys.getPaySecret())) {
			return "paySecret未配置";
		}

		// 注意：这里返回的备注信息=机构号
		// 根据机构编号查找机构的扩展信息
		String sign = MerchantApiUtil.getSign(paramMap, sys.getPaySecret());
		if (!sign.equals(callbackOrderBO.getSign())) {
			logger.error("签名前参数={}，签名sign={}，签名secret={}", paramMap, sign, sys.getPaySecret());
			return "fail";
		}

		// 转换支付回调返回的实体数据
		OrderInfoPayNotifyBO payNotifyBO = new OrderInfoPayNotifyBO();
		// 注意：这里返回的交易号=订单的流水号
		payNotifyBO.setSerialNumber(Long.valueOf(callbackOrderBO.getOutTradeNo()));
		// 状态转换
		if (TradeStatusEnum.SUCCESS.getCode().equals(callbackOrderBO.getTradeStatus())) {
			payNotifyBO.setOrderStatus(OrderStatusEnum.SUCCESS.getCode());
		} else if (TradeStatusEnum.FAILED.getCode().equals(callbackOrderBO.getTradeStatus())) {
			payNotifyBO.setOrderStatus(OrderStatusEnum.FAIL.getCode());
		}

		return biz.payNotify(payNotifyBO).getData();
	}
}
